package com.tianqu.tms.www.rental.controller;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.RentalContractInfo;
import com.tianqu.tms.www.rental.viewhelper.RentalContractAddInputViewHelper;

/**
 * 合同添加 - 信息录入 controller
 * @author mufeiyu
 */
public class RentalContractAddInputController extends TmsController {
	private Logger logger = Logger.getLogger(RentalContractAddInputController.class);
	RentalContractAddInputViewHelper contractSerch = new RentalContractAddInputViewHelper();

	@SuppressWarnings("unchecked")
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			RentalContractInfo contractInfo = RentalContractInfo.getInstance();
			
			view(contractInfo);
			uc.setAttribute(TmsConstants.VIEWHELP, contractSerch);
			logger.debug(this.getClass().getName() + "#execute was completed.");
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

	}

	/**
	 * 赋值转换
	 * 
	 * @param contractInfo
	 * @return
	 * @throws TmsDaoException 
	 */
	private RentalContractAddInputViewHelper view(RentalContractInfo contractInfo) throws TmsDaoException {
		
		//合同ID
		contractSerch.setId(contractInfo.getId());
		//合同编号
		contractSerch.setSerial(contractInfo.getSerial());
		//租金
		if(contractInfo.getRent() == 0){
			contractSerch.setRent("");
		} else {
			contractSerch.setRent(String.valueOf(contractInfo.getRent()));
		}		
		//合同开始日期
		contractSerch.setStartDate(DateUtil.getDateByTimeStamp(contractInfo.getStartDate()));
		//合同结束日期
		contractSerch.setEndDate(DateUtil.getDateByTimeStamp(contractInfo.getEndDate()));
		//租位编号
		contractSerch.setLocationSerial("");
		//租位类型
		contractSerch.setRentalType(contractInfo.getRentalType());
		//租位面积(㎡)
		contractSerch.setAcreage("");
		//车位数量
		contractSerch.setCarPort("");
		//合同人姓名
		contractSerch.setOwnerName(contractInfo.getOwnerName());
		//身份证号码
		contractSerch.setOwnerIdCardNo(contractInfo.getOwnerIdCardNo());
		//性别
		contractSerch.setOwnerSex("男");
		//出生日期
		contractSerch.setOwnerBirthday(DateUtil.getDateByTimeStamp(contractInfo.getOwnerBirthday()));
		//联系电话
		contractSerch.setOwnerPhone(contractInfo.getOwnerPhone());
		//地址
		contractSerch.setOwnerAddress(contractInfo.getOwnerAddress());
		//备注
		contractSerch.setRemark(contractInfo.getRemark());
		//资料图片
		contractSerch.setPhoto_1(contractInfo.getPhoto_1());
		contractSerch.setPhoto_2(contractInfo.getPhoto_2());
		contractSerch.setPhoto_3(contractInfo.getPhoto_3());
		contractSerch.setPhoto_4(contractInfo.getPhoto_4());
		contractSerch.setPhoto_5(contractInfo.getPhoto_5());
		contractSerch.setPhoto_6(contractInfo.getPhoto_6());
		contractSerch.setPhoto_7(contractInfo.getPhoto_7());
		contractSerch.setPhoto_8(contractInfo.getPhoto_8());
		return contractSerch;
	}
}
